<?php

class DefaultController extends BaseController {
    
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->createUrl('site/index'));
    }
    
    public function actionSignUp() {
        if(!Yii::app()->user->isGuest) $this->redirect(array('profile/profile', 'login'=>$model->login));
        $model = new User();
        if(isset ($_POST['User'])) {
            $model->attributes = $_POST['User'];
            $model->setScenario('reg');
            if($model->save()) {
                $identity = new UserIdentity($model->login, $model->password);
                $identity->authenticate();
                Yii::app()->user->login($identity);
                $this->redirect(array('profile/profile', 'login'=>$model->login));
            }
        }
        $this->render('signUp', array('model'=>$model));
    }
    
    public function actionLogin($uloginErrors='') {
        if(!Yii::app()->user->isGuest) $this->redirect(array('profile/profile', 'login'=>Yii::app()->user->getState('login')));
        $model = new Login();
        $uloginErrors = unserialize($uloginErrors);
        if(!empty($uloginErrors)) {
            for($i=0; $i<count($uloginErrors['uloginErrors']); $i++) $model->addError('', $uloginErrors['uloginErrors'][$i]);
        }
        if (isset($_POST['Login'])) {
            $model->attributes = $_POST['Login'];
            if($model->loginUser()) {
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        $this->render('login', array('model'=>$model));
    }
}